package com.yl.immodule.widget;

import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.yl.immodule.R;
import com.yl.immodule.util.TimeUtils;
import com.yl.immodule.v.models.ChatMessageViewModel;
import com.hm.poc.chat.ChatMessageCategory;
import com.hm.poc.chat.ChatMessageStatus;
import com.unionbroad.greendao.ChatMessage;
import com.unionbroad.app.util.BusinessUtil;


public class ChatMessageViewHolder extends BaseChatMessageViewHolder {
    private static final String TAG = "ChatMessageViewHolder";

    ImageView iv;
    TextView name;
    ChatMessageContainer container;
    View containerRoot;
    TextView time_stamp;
    View status_layout;
    ImageView status_icon;
    TextView status_tip;
    ImageView unread_icon;
    ProgressBar sending_progressBar;
    private long chatId;
    private ChatMessageViewModel mViewModel;
    private ChatMessageViewAdapter adapter;

    public ChatMessageViewHolder(View itemView, long chatId, ChatMessageViewModel viewModel) {
        super(itemView);
        this.chatId = chatId;
        this.mViewModel = viewModel;
        iv = (ImageView) itemView.findViewById(R.id.icon);
        name = (TextView) itemView.findViewById(R.id.name);
        container = (ChatMessageContainer) itemView.findViewById(R.id.content);
        containerRoot = itemView.findViewById(R.id.content_root);
        time_stamp = (TextView) itemView.findViewById(R.id.time_stamp);
        status_layout = itemView.findViewById(R.id.status_layout);
        status_icon = (ImageView) itemView.findViewById(R.id.status_icon);
        status_tip = (TextView) itemView.findViewById(R.id.status_tip);
        unread_icon = (ImageView) itemView.findViewById(R.id.unread_icon);
        sending_progressBar = (ProgressBar) itemView.findViewById(R.id.sending_progressbar);
        container.setViewModel(mViewModel);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void bindViewData(ChatMessage message, boolean timeDivision) {
        // 记录消息ID和是否是发送消息，便于调试
        Log.d(TAG, "绑定消息: ID=" + message.getId() + ", 发送=" + message.getIs_out() + ", 类型=" + message.getCategory() + ", 进度=" + message.getUpdown_progress());
        
        // 处理群聊名称显示
        if (message.getIs_out() || !BusinessUtil.isGroup(message.getChat_id())) {
            name.setVisibility(View.GONE);
        } else {
            name.setText(message.getRemote_name() != null ? message.getRemote_name() : String.valueOf(message.getRemote_number()));
            name.setVisibility(View.VISIBLE);
        }

        // 确保消息的位置是根据消息是否为发送消息决定的，而不是根据其他状态
        // 这是关键修复：无论上传状态如何，只根据是否是发出的消息决定其位置
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) containerRoot.getLayoutParams();
        if (!message.getIs_out()) {
            // 接收消息 - 左侧
            iv.setVisibility(View.VISIBLE);
            lp.leftMargin = 0;
            lp.rightMargin = 60;
            sending_progressBar.setVisibility(View.GONE);
            status_icon.setVisibility(View.GONE);
        } else {
            // 发送消息 - 右侧
            iv.setVisibility(View.VISIBLE);
            lp.leftMargin = 60;
            lp.rightMargin = 0;
        }
        containerRoot.setLayoutParams(lp);

        // 显示时间
        if (timeDivision) {
            time_stamp.setText(TimeUtils.formatTime(message.getTime_stamp()));
            time_stamp.setVisibility(View.VISIBLE);
        } else {
            time_stamp.setVisibility(View.GONE);
        }

        // 显示发送状态
        int sip_status = message.getSip_status() == null ? ChatMessageStatus.Sip.UNKNOW : message.getSip_status();
        if (message.getIs_out()) { // 只有发送消息才显示状态
            switch (sip_status) {
                case ChatMessageStatus.Sip.UNKNOW:
                case ChatMessageStatus.Sip.FAILED:
                    sending_progressBar.setVisibility(View.GONE);
                    status_tip.setVisibility(View.GONE);
                    status_icon.setImageResource(R.drawable.ic_chat_message_send_failed);
                    status_icon.setVisibility(View.VISIBLE);
                    break;
                case ChatMessageStatus.Sip.SENDING:
                    status_icon.setVisibility(View.GONE);
                    status_tip.setVisibility(View.GONE);
                    sending_progressBar.setVisibility(View.VISIBLE);
                    break;
                case ChatMessageStatus.Sip.SENDED:
                    if (message.getCategory() == ChatMessageCategory.AUDIO_FILE) {
                        status_tip.setVisibility(View.VISIBLE);
                        status_tip.setText(message.getDuration() + "″");
                    } else {
                        status_tip.setVisibility(View.GONE);
                    }
                    sending_progressBar.setVisibility(View.GONE);
                    status_icon.setVisibility(View.GONE);
                    break;
                default:
                    status_icon.setVisibility(View.GONE);
                    sending_progressBar.setVisibility(View.GONE);
                    break;
            }
        }
        
        // 未读状态
        int chat_status = message.getMsg_status() == null ? ChatMessageStatus.Chat.UNKNOW : message.getMsg_status();
        switch (chat_status) {
            case ChatMessageStatus.Chat.UNREAD:
                if (message.getCategory() == ChatMessageCategory.AUDIO_FILE || message.getCategory() == ChatMessageCategory.VIDEO_FILE) {
                    unread_icon.setVisibility(View.VISIBLE);
                } else {
                    unread_icon.setVisibility(View.GONE);
                }
                break;
            default:
                unread_icon.setVisibility(View.GONE);
                break;
        }

        // 绑定消息内容
        container.bindChatContent(message);
    }

    @Override
    public void setOnClickListener(final View.OnClickListener l) {
        container.setOnClickListener(l);
    }

    @Override
    public void setOnLongClickListener(View.OnLongClickListener l) {
        container.setOnLongClickListener(l);
    }

    @Override
    public void setOnResendEventListener(View.OnClickListener l) {
        status_icon.setOnClickListener(v -> {
            // 获取当前消息
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION && adapter != null) {
                ChatMessage message = adapter.getDataByPosition(position);
                if (message != null && message.getSip_status() == ChatMessageStatus.Sip.FAILED) {
                    // 使用ViewModel重发消息
                    mViewModel.sendMessage(message);
                }
            }
            
            // 保留原来的回调逻辑
            if (l != null) {
                l.onClick(v);
            }
        });
    }

    public void setAdapter(ChatMessageViewAdapter adapter) {
        this.adapter = adapter;
    }
}